/*
Copyright (c) 2008, Oleg S. Estekhin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the copyright holders nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package ebml.viewer.tree;

import java.util.Enumeration;

import javax.swing.Icon;
import javax.swing.tree.TreeNode;

/** Provides an extended functionality over the standard <code>TreeNode</code>. */
public abstract class ExtendedTreeNode implements TreeNode {

    private TreeNode parent;

    private TreeNode[] children;

    private Icon icon;


    /** Creates a new <code>ExtendedTreeNode</code> object. */
    protected ExtendedTreeNode() {
    }

    /**
     * Creates a new <code>ExtendedTreeNode</code> object.
     *
     * @param icon the icon or <code>null</code> if this node has no icon
     */
    protected ExtendedTreeNode(Icon icon) {
        this.icon = icon;
    }

    /**
     * Creates a new <code>ExtendedTreeNode</code> object.
     *
     * @param parent the parent node or <code>null</code> if this node has no parent
     * @param children the children or <code>null</code> if this node does not allow children
     * @param icon the icon or <code>null</code> if this node has no icon
     */
    protected ExtendedTreeNode(TreeNode parent, TreeNode[] children, Icon icon) {
        this.parent = parent;
        this.children = children;
        this.icon = icon;
    }


    @Override
    public TreeNode getParent() {
        return parent;
    }

    /**
     * Changes the parent node.
     *
     * @param parent the new parent node
     */
    public void setParent(TreeNode parent) {
        this.parent = parent;
    }


    /**
     * Returns the children nodes.
     *
     * @return the children nodes or <code>null</code> if the node does not allow children
     */
    public TreeNode[] getChildren() {
        return children;
    }

    /**
     * Changes the children nodes.
     *
     * If the array contains <code>ExtendedTreeNode</code> elements then their parent node will be changed to this
     * node.
     *
     * @param children the new children nodes
     */
    public void setChildren(TreeNode[] children) {
        this.children = children;
        if (children != null) {
            for (TreeNode child : children) {
                if (child instanceof ExtendedTreeNode) {
                    ExtendedTreeNode node = (ExtendedTreeNode) child;
                    node.setParent(this);
                }
            }
        }
    }

    /**
     * Returns the node icon.
     *
     * @return the node icon or <code>null</code> if the node does not have a special icon
     */
    public Icon getIcon() {
        return icon;
    }

    /**
     * Changes the node icon.
     *
     * @param icon the new icon
     */
    public void setIcon(Icon icon) {
        this.icon = icon;
    }

    /**
     * Returns the node text.
     *
     * @return the node text
     */
    public abstract String getText();


    @Override
    public boolean getAllowsChildren() {
        return children != null;
    }

    @Override
    public boolean isLeaf() {
        return children == null;
    }


    @Override
    public int getChildCount() {
        return children == null ? 0 : children.length;
    }

    @Override
    public TreeNode getChildAt(int index) {
        return children[index];
    }

    @Override
    public int getIndex(TreeNode node) {
        int index = children.length - 1;
        while (index >= 0 && children[index] != node) {
            index--;
        }
        return index;
    }


    @Override
    public Enumeration<?> children() {
        throw new UnsupportedOperationException();
    }


    @Override
    public String toString() {
        return getText();
    }

}
